<?php
class System extends WebBase{

    public function __construct()
    {
        parent::__construct();
    }

    /*
     * 设置中心
     */
    public function index(){
        global $_W,$_GPC;

        if($_W['isajax']){

            $data['site_name'] = $_GPC['site_name'];
            $data['copyright'] = $_GPC['copyright'];
            if (!empty($_GPC['site_logo'])) {
                if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $_GPC['site_logo'], $result)){
                    $ext = $result[2];

                    if (!is_dir(UPLOAD_LOGO)) {
                        if (!mkdir(UPLOAD_LOGO, 0777, true)) {
                            $this->outjson(array('code' => 102, 'msg' => '文件夹还没开放权限'));
                        }
                    }
                    $m = time() . mt_rand(100, 999);
                    $full_name = UPLOAD_LOGO . 'logo' .$m . '.'. $ext;
                    $base64_img_url = base64_decode(str_replace($result[1], '', $_GPC['site_logo']));
                    if (file_put_contents($full_name, $base64_img_url)) {
                        $data['site_logo'] = $this->modulename . '/logo/logo' . $m . '.' . $ext;
                    } else {
                        $this->outjson(array('code' => 405, 'msg' => '上传图片失败: 没法生成图片'));
                    }
                } else {
                    $data['site_logo'] = getSite('site_logo');
                }
            }
            $data['stats_code'] = empty($_GPC['stats_code']) ? '' : htmlspecialchars($_GPC['stats_code']);
            $data['redirect_url'] = empty($_GPC['redirect_url']) ? '' : $_GPC['redirect_url'];
            $data['appid'] = empty($_GPC['appid']) ? '' : $_GPC['appid'];
            $data['secret'] = empty($_GPC['secret']) ? '' : $_GPC['secret'];

            $data['open_qiniu'] = $_GPC['open_qiniu'];
            $data['qiniu_access_key'] = $_GPC['qiniu_access_key'];
            $data['qiniu_secrey_key'] = $_GPC['qiniu_secrey_key'];
            $data['qiniu_bucket'] = $_GPC['qiniu_bucket'];
            $data['qiniu_url'] = $_GPC['qiniu_url'];
            $data['qiniu_uploadmax'] = $_GPC['qiniu_uploadmax'];

            $data['workorder_msg'] = $_GPC['workorder_msg'];
            $data['receive_msg'] = $_GPC['receive_msg'];
            $data = serialize($data);
            $setting['value'] = $data;
            if(pdo_get('wherein_knowledge_setting',array('uniacid'=>$_W['uniacid']))){
                //修改
                $update = pdo_update('wherein_knowledge_setting', $setting, array('uniacid'=>$_W['uniacid']));
                if ($update) {
                    $res['msg'] = '修改成功';
                    $res['code'] = 200;
                } else {
                    $res['msg'] = '修改失败';
                    $res['code'] = 304;
                }
            } else {
                //添加
                $setting['uniacid'] = $_W['uniacid'];
                $insert = pdo_insert('wherein_knowledge_setting',$setting);
                if($insert){
                    $res['msg'] = '添加成功';
                    $res['code'] = 200;
                }else{
                    $res['msg'] = '添加失败';
                    $res['code'] = 304;
                }
            }
            $this->outjson($res);
        }

        $set = pdo_get('wherein_knowledge_setting', array('uniacid'=>$_W['uniacid']));
        $settings = !empty($set) ? unserialize($set['value']) : array();

        include $this->template('index');

    }

}